package com.gtja.beoms.xxljob.handle;


import com.gtja.beoms.base.CommonConstants;
import com.gtja.beoms.base.CosUrlConstants;
import com.gtja.beoms.service.IFinanceService;
import com.gtja.beoms.utils.HttpRequestUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * <p>
 *
 * </p>
 *
 * @author CP_wangzhi
 * @since 2021/10/13
 */
@Component
public class JobHandle {
    private Logger logger = LoggerFactory.getLogger(JobHandle.class);

    @Autowired
    private IFinanceService iFinanceService;

    @Autowired
    private CosUrlConstants cosUrlConstants;



    /**
     * <p>
     * 会话存档定时任务
     * </p>
     *
     * @return ReturnT
     * @author CP_wangzhi
     * @since 2021/10/15
     */
    @XxlJob("conversationArchiveHandle")
    public ReturnT<String> syncConversationArchiveTask() throws Exception {
        // 获取参数
        String param = XxlJobHelper.getJobParam();
        String[] methodParams = param.split(",");
        String corpId = methodParams[0];
        if (StringUtils.isBlank(corpId)) {
            XxlJobHelper.log("corpId不能为空!");
            logger.error("corpId不能为空!");
            return ReturnT.FAIL;
        }
        logger.info("接收调度中心参数...[{}]", param);
        //控制台输出日志
        logger.info("Session archive timed task execute...");
        try {
            //书写业务逻辑
            iFinanceService.syncFinance(corpId, methodParams);
            //写日志到调度中心日志中
            XxlJobHelper.log("会话存档定时任务 execute Success...");
            // 设置任务结果
            return ReturnT.SUCCESS;
        } catch (Exception e) {
            XxlJobHelper.log("会话存档定时任务 execute Fail...");
            logger.error("会话存档定时任务 execute Fail ...");
            String url = cosUrlConstants.SEND_SMS;
            HashMap<String, Object> params = new HashMap<>(4);
            params.put("mobile", CommonConstants.GH_PHONE_NUMBER);
            params.put("content", "定时任务报错");
            logger.info("请求cos发送短信预警url：[{}]", url);
            String response = HttpRequestUtil.huToolPost(url, params);
            logger.info("请求发送短信响应结果[{}]", response);
            throw e;
//            logger.error("会话存档定时任务 execute Fail ...");
//            //写日志到调度中心日志中
//            XxlJobHelper.log("会话存档定时任务 execute Fail...");
//            //发送邮件
//            // 设置任务结果
//            return ReturnT.FAIL;
        }
    }
}
